# 第一阶段：构建及安装依赖
FROM registry.cn-chengdu.aliyuncs.com/yiweitang/playwright:v1.44.0-jammy

# 作者信息
LABEL maintainer="yiweitang@yunify.com" version="0.1"

# 设置镜像源为阿里云
RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' > /etc/timezone && \
    pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

# 创建docker默认用户
RUN useradd tyw

# 设置工作目录
WORKDIR /app

# 安装Poetry
RUN pip install --upgrade pip && \
    pip install poetry

# 复制项目文件到容器中
COPY pyproject.toml poetry.lock ./

# 使用Poetry安装生产依赖
RUN poetry config virtualenvs.create false && \
    poetry install --no-dev --no-interaction --no-ansi

# 设置环境变量，确保Python能找到虚拟环境中的库
ENV PATH="/app/.venv/bin:$PATH"

# 备份源
RUN cp /etc/apt/sources.list /etc/apt/sources.list.bak

# 设置阿里云源
RUN sed -i 's@archive.ubuntu.com/@mirrors.aliyun.com/@g' /etc/apt/sources.list && \
    sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list && \
    sed -i 's@security.ubuntu.com/@mirrors.aliyun.com/@g' /etc/apt/sources.list

# 安装Playwright浏览器所需的系统依赖
RUN apt-get update && \
    apt-get install -y \
        libglib2.0-0 \
        libnss3 \
        libnspr4 \
        libdbus-1-3 \
        libatk1.0-0 \
        libatk-bridge2.0-0 \
        libcups2 \
        libdrm2 \
        libatspi2.0-0 \
        libx11-6 \
        libxcomposite1 \
        libxdamage1 \
        libxext6 \
        libxfixes3 \
        libxrandr2 \
        libgbm1 \
        libxcb1 \
        libxkbcommon0 \
        libpango-1.0-0 \
        libcairo2 \
        libasound2 \
        vim \
        wget \
        curl \
        iproute2 \
        unzip \
        iputils-ping  \
        ttf-wqy-microhei && \
    rm -rf /var/lib/apt/lists/*

# 复制应用源代码到容器中
COPY . .

# 运行你的测试脚本
CMD ["python", "./main.py"]